import _ from "lodash";
import { Behavior } from "../behavior/Behavior";
import { EnumBehaviorActionType } from "../behavior/EnumBehaviorActionType";
import { EnumBehaviorReturn } from "../behavior/EnumBehaviorReturn";
import { RobotEntity } from "../RobotEntity";
import { EnumBehaviorType } from "./EnumBehaviorType";
import { EnumErrorCode } from "../../../../../common/enum/EnumErrorCode";
import { GameRoomInfo } from "../../../../../common/model/GameRoomInfo";
import { CS_EnterGameRoom, SC_EnterGameRoom } from "../../../../../common/protocols/Proto_EnterGameRoom";
import { CS_GetRoomInfo, SC_GetRoomInfo } from "../../../../../common/protocols/Proto_GetRoomInfo";
import { CS_GetRoomList, SC_GetRoomList } from "../../../../../common/protocols/Proto_GetRoomList";
import { CS_Bet, SC_Bet } from "../../../../../common/protocols/Proto_Bet";
import { Tools } from "../../../../../framework/utils/Tools";
import { GameBetOrderInfo } from "../../../../../common/model/GameBetOrderInfo";


export class BehaviorVote extends Behavior {

    public type: EnumBehaviorType = EnumBehaviorType.Vote;

    public actionType: EnumBehaviorActionType = EnumBehaviorActionType.Selector;

    constructor() {
        super();

    }

    public condition(): boolean {

        const entity: RobotEntity = this.getEntity();
        return this.actionState != EnumBehaviorReturn.RUNNING && this.entity.isLogined();

    }

    public action(): EnumBehaviorReturn {

        this.actionState = EnumBehaviorReturn.RUNNING;
        //**先查询房间是否有可以下注的 */


        if (this.entity._data.roomId.length === 0) {
            //**需要先进入房间 */

            const cs_getRoom: CS_GetRoomList = new CS_GetRoomList();

            this.entity.sendMsg(cs_getRoom, (sc: SC_GetRoomList) => {

                if (sc.errorcode === EnumErrorCode.OK) {
                    const room: GameRoomInfo = _.sample(sc.list);

                    this.entity.setRoomList(sc.list);
                    this.enterRoom(room.uid);
                }
            });
        }
        else {
            this.getRoomInfo(this.entity._data.roomId);
        }

        return this.actionState;
    }

    private enterRoom(roomid: string): void {
        const cs: CS_EnterGameRoom = new CS_EnterGameRoom();
        cs.roomid = roomid;
        this.entity.sendMsg(cs, (sc: SC_EnterGameRoom) => {
            if (sc.errorcode === EnumErrorCode.OK) {
                this.entity.setCurRoom(sc.room);
                this.entity._data.roomId = sc.room.uid;
            }
        });
    }


    private getRoomInfo(roomid: string): void {
        const cs: CS_GetRoomInfo = new CS_GetRoomInfo();
        cs.roomid = roomid;
        this.entity.sendMsg(cs, (sc: SC_GetRoomInfo) => {
            if (sc.errorcode === EnumErrorCode.OK) {
                this.entity.setCurRoom(sc.data);
                this.vote(sc.data);
            }
        });
    }


    private vote(room: GameRoomInfo): void {

        const order = _.find(room.orders, { userid: this.entity._userId });

        if (order) {
            //**已经下注了则无需再次下注 */
            this.actionState = EnumBehaviorReturn.SUCCESS;
            return;
        }

        const cs: CS_Bet = new CS_Bet();
        cs.roomid = room.uid;

        this.entity.sendMsg(cs, (sc: SC_Bet) => {
            if (sc.errorcode === EnumErrorCode.OK) {
                this.entity._data.coins = sc.coin;
                this.entity._data.orders.push(sc.order);
            }
            else {

            }
        });
    }

    private canVote(): boolean {

        const roomList: GameRoomInfo[] = this.entity.getRoomList();
        const room: GameRoomInfo = _.find(roomList, { uid: this.entity._data.roomId });

        if (room.expireTime - Tools.getServerTime() > 0 && this.entity._data.coins >= 500) {
            const voteCoin: number = Math.ceil(10 * Math.random()) * this.entity._data.coins / 100;
            
            const betInfo:GameBetOrderInfo = new GameBetOrderInfo();
            betInfo.prizeID = room.prizeId;
            betInfo.bets.push({key:3,coin:voteCoin});

            

        }
        return true;
    }
}